package com.wenhai.leetcode.set;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

/**
 *https://leetcode-cn.com/problems/intersection-of-two-arrays/
 *
 * @author wenhai
 * @date   2021/3/8
 */
public class _349两个数组的交集 {

    public int[] intersection(int[] nums1, int[] nums2) {
        TreeSet<Integer> set = new TreeSet<>();
        List<Integer> resultList = new ArrayList<>();
        for (int num : nums1) {
            set.add(num);
        }

        for (int num : nums2) {
            if (set.contains(num)) {
                resultList.add(num);
                set.remove(num);
            }
        }
        int[] resultArr = new int[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {
            resultArr[i] = resultList.get(i);
        }
        return resultArr;
    }

    public static void main(String[] args) {

    }

}
